Skip to content

Optimize branch instructions to short form at known-small-distance sites#529

Merged
dadhi merged 5 commits intomasterfrom
copilot/optimize-branches-to-small-branches
Apr 27, 2026
Merged

Optimize branch instructions to short form at known-small-distance sites#529
dadhi merged 5 commits intomasterfrom
copilot/optimize-branches-to-small-branches

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 22, 2026

Branch instructions like Br, Brtrue, Brfalse encode a 4-byte offset by default, but have short-form variants (Br_S, Brtrue_S, Brfalse_S) using a 1-byte signed offset when the target is within ±127 bytes. Previously, a blanket replacement broke large switch statements. This PR applies short-form branches only at specific ad-hoc sites where the distance to the target is provably tiny.

Changes

Specific forward-branch optimizations (always safe, hardcoded)

Patterns where the skipped IL is provably small enough to always use the short form:

Site Change Max skipped bytes
IsFalse unary Brfalse→Brfalse_S, Br→Br_S 3
Reference coalesce with cast Br→Br_S 5 (Castclass)
isLiftedToNull comparison Brfalse→Brfalse_S, Brtrue→Brtrue_S 6
Logical operator labelDone Br→Br_S 1 (Ldc_I4_0/1)

Tests updated

One existing opcode-assertion test updated to expect the new short-form opcode where the optimization applies (Issue159).

Copilot AI linked an issue Apr 22, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Optimize branches to small branches Optimize branch instructions to short form where branch distance fits in sbyte Apr 22, 2026
Copilot AI requested a review from dadhi April 22, 2026 07:02
public static bool DisableDemit;

// Tracks the IL offset of each marked label per ILGenerator, used by smart branch emitters to select short form when possible
internal static readonly ConditionalWeakTable<ILGenerator, Dictionary<Label, int>> _ilLabelPositions =
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Baa idea unless we limit it being fully on stack and inlined etc. Eg. if you use Stack8 or 16 and overwrite the older indexes in LRU fasion. You don't need weak table is everything is on stack.
If this kind of sinplification is impossible then drop this generalized optimization. We will keep only adhoc places where we know the distances and can apply smsll branches on the spot.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped the ConditionalWeakTable-based general optimizer entirely (commit f4fc8ab). The PR now only contains the ad-hoc forward-branch short-form replacements for the four known-small patterns where the distance to the target is provably tiny (≤6 bytes): IsFalse, coalesce-with-cast, isLiftedToNull, and logical-operator labelDone.

Copilot AI changed the title Optimize branch instructions to short form where branch distance fits in sbyte Optimize branch instructions to short form at known-small-distance sites Apr 27, 2026
Copilot AI requested a review from dadhi April 27, 2026 15:15
@dadhi dadhi marked this pull request as ready for review April 27, 2026 17:07
@dadhi dadhi merged commit 307f9a1 into master Apr 27, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize Branches to Small Branches

2 participants